<?php

namespace Home\Controller;

/**
 * Description of HotelController.class控制器实例
 * 
 * @author songanwei
 * @date 2016-1-12
 */
class HotelController extends ParentController {

    /**
     * @description 客栈列表
     * @modify songanwei 2016-01-12
     */
    public function index() {
        $param = I('post.');
        $where = $condition = array();
        if (isset($param['k']) && $param['k']) {
            $condition['k'] = $param['k'];
            $where['_string'] = "name like '%" . $param['k'] . "%'";
        }
        $hotelEntityModel = D('HotelEntity');

        $count = $hotelEntityModel->where($where)->count();

        $Page = classpage($count);
        $order = 'createtime desc';
        $hotelData = $hotelEntityModel->where($where)->order($order)->limit($Page->firstRow, $Page->listRows)->select();
        foreach ($hotelData as &$row) {
            $row['cityname'] = explode('/', D('City')->getName($row['city_id']))[0];
            $row['areaname'] = explode('/', D('District')->getName($row['district_id']))[0];
            $row['enddate_style'] = strtotime($row['enddate'])<time()?'red':'green';
        }

        $page = $Page->show();

        $this->assign('page', $page);
        $this->assign('hotelData', $hotelData);

        $this->display();
    }

    /**
     * @description 更新服务到期时间
     * @modify songanwei 2016-03-17
     */
    public function setDate() {
        $hotelId = I('post.id');
        $endDate = I('post.enddate');
        $usage = I('post.usage', 2, 'intval');
        $roomLimit = I('post.roomlimit', 0, 'intval');
        $endDate = $endDate ? $endDate : '0000-00-00';
        $hotelEntityModel = D('HotelEntity');
        $result = $hotelEntityModel->setDate($hotelId, $endDate, $usage, $roomLimit);
        if ($result == false) {
            $this->returnJson(403, 506);
        }
        $hotelDetailModel = D('HotelDetail');
        $hotelDetailModel->where(array('hotelentity_id'=>$hotelId))->save(array('enddate'=>$endDate));

        // 修改对应的接口服务
        $interfaceServerLogic = A('Common/InterfaceServer', 'Logic');
        $saveResult = $interfaceServerLogic->saveInterfaceServer($hotelId, $usage, $endDate);
        if ($saveResult == false) {
            $this->returnJson(500, 506);
        }

        $this->returnJson(200, 200);
    }

    /**
     * @description 设置酒店数量
     * @modify zhangzhen 2017-03-28
     */
    public function setNum() {
        $num = I('post.rest', 0, 'intval');
        $redisUtil = new \Redis();
        $redisUtil->connect(C('REDIS_SERVERIP'), C('REDIS_SERVERPORT'));
        $redisUtil->auth(C('REDIS_AUTH_PASSWORD'));
        $result = $redisUtil->set('hotelrest', $num);
        if ($result == false) {
            $this->returnJson(403, 506);
        }
        $this->returnJson(200, 200, $num);
    }

    /**
     * @description 获取酒店数量
     * @modify zhangzhen 2017-03-28
     */
    public function getNum() {
        $redisUtil = new \Redis();
        $redisUtil->connect(C('REDIS_SERVERIP'), C('REDIS_SERVERPORT'));
        $redisUtil->auth(C('REDIS_AUTH_PASSWORD'));
        $result = $redisUtil->get('hotelrest');
        if (empty($result)) {
            $this->returnJson(200, 200, '');
        } else {
           $this->returnJson(200, 200, $result); 
       }
    }

    /**
     * @description 客栈义卖统计
     * @modify wenhui 2017-09-21
     */
    public function charityHotel() {
        $param = I('post.');

        $where = $condition = array();
        if (isset($param['k']) && $param['k']) {
            $condition['k'] = $param['k'];
            $where['_string'] = "name like '%" . $param['k'] . "%'";
        }

        $hotelEntityModel = D('HotelEntity');
        $hotelInfo = $hotelEntityModel->where($where)->select();

        $hotelInfo = getArrayValueAsIndex($hotelInfo, 'id');
        $hotelIds = array_column($hotelInfo, 'id');
        if (empty($hotelIds)) {
            $this->display();
            return;
        }

        $option['hotelentity_id'] = array('in', $hotelIds);
        $option['status'] = array('in', [1,2,3,4]);
        $option['charityprice'] = array('neq', 0);
        if($param['s'] && $param['e']){
            $endDate = $param['e'] . ' 23:59:59';
            $option['createtime'] =  array('between',[$param['s'], $endDate]);
        }else if($startDate){
            $option['createtime'] =  array('egt',$param['s']);
        }

        // 获取客栈默认货币
        $currencyModel = D('Currency');
        $currencyHotelModel = D('CurrencyHotel');
        $currencyInfo = $currencyModel->getCurrencyInfo();
        $currencyInfo = getArrayValueAsIndex($currencyInfo, 'id');
        $currencyHotelInfo = $currencyHotelModel->getDefaultCurrency($hotelIds);
        $currencyHotelInfo = getArrayValueAsIndex($currencyHotelInfo, 'hotelentity_id');

        $orderInfoModel = D('OrderInfo');
        $group = 'hotelentity_id';
        $orderInfo = $orderInfoModel->field("hotelentity_id,sum(charityprice) as charityprice")->where($option)->group($group)->select();
        $adminHotelModel = D('AdminHotel');
        $adminLoginModel = D('AdminLogin');
        // 补充客栈名称 掌柜名称 手机号
        foreach ($orderInfo as $key => &$orderInfoItem) {
            $adminHotelInfo = $adminHotelModel->getInfosByHotel($orderInfoItem['hotelentity_id']);
            $adminLoginInfo = $adminLoginModel->getInfo($adminHotelInfo[0]['adminlogin_id']);

            if (isset($currencyHotelInfo[$orderInfoItem['hotelentity_id']])) {
                $orderInfoItem['standsymbol'] = $currencyInfo[$currencyHotelInfo[$orderInfoItem['hotelentity_id']]['currency_id']]['symbol'] . '(' . $currencyInfo[$currencyHotelInfo[$orderInfoItem['hotelentity_id']]['currency_id']]['standsymbol'] . ')';
            } else {
                // 默认人民币
                $orderInfoItem['standsymbol'] = $currencyInfo[8]['symbol'] . '(' . $currencyInfo[8]['standsymbol'] . ')';
            }

            $orderInfoItem['hotelName'] = $hotelInfo[$orderInfoItem['hotelentity_id']]['name'];
            $orderInfoItem['adminName'] = $adminLoginInfo['name'];
            $orderInfoItem['mobile'] = '+' . $adminLoginInfo['areacode'] . ' ' . $adminLoginInfo['mobile'];
        }
        unset($orderInfoItem);

        $pageIndex = isset($_GET['p']) ?$_GET['p'] : 1;
        // 查找不分页
        $pageIndex = isset($param['z']) && $param['z'] == 1 ? '' : $pageIndex;  
        $hotelData = generatePageInfo($orderInfo, $pageIndex, 20);

        $Page = classpage($hotelData['page']['total'], 20);
        $Page->totalPages = $hotelData['page']['pageTotal'];
        $Page->parameter = array('p' => $hotelData['page']['current']);
        $page = $Page->show();

        $this->assign('page', $page);
        $this->assign('hotelData', $hotelData['hotelInfo']);

        $this->display();
    }
}
